package com.lovo.csc.service.impl;

import com.lovo.csc.entity.Offerinfo;
import com.lovo.csc.mapper.OfferinfoMapper;
import com.lovo.csc.service.IOfferinfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lovo.psc.dto.PriceDto;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.stereotype.Service;

import java.io.IOException;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lovo
 * @since 2022-07-13
 */
@Service
public class OfferinfoServiceImpl extends ServiceImpl<OfferinfoMapper, Offerinfo> implements IOfferinfoService {

    @Autowired
    IOfferinfoService offerinfoService;

    @RabbitListener(queues = "PRICEQUEUE")
    public void saveOfferInfo(Channel channel, @Header(AmqpHeaders.DELIVERY_TAG) long tag, PriceDto dto) throws IOException {
        if (dto!=null){
            Offerinfo offerinfo = new Offerinfo();
            offerinfo.setSupplierid(Integer.parseInt(dto.getSupplierNumber()));
            offerinfo.setSuppliername(dto.getSupplierName());
            offerinfo.setGoodstate("未审核");
            offerinfo.setPurchasenum(Integer.parseInt(dto.getNum()));
            offerinfo.setSuppleroffer(Integer.valueOf(dto.getPrice()));
            offerinfo.setPurchasetendercode(Integer.valueOf(dto.getBookNumber()));
            offerinfo.setSupplerinfo(dto.getInfo());
            offerinfo.setPurchasetenderid(Integer.valueOf(dto.getBookNumber()));
            offerinfoService.save(offerinfo);
            channel.basicAck(tag,false);
        }
    }

    @Override
    public void updateOfferState(int OfferId, String state) {
        baseMapper.updateOfferState(OfferId,state);
    }
}
